package com.dlc.yunguang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.utils.RecyclerViewUtil;
import com.dlc.commonlibrary.widget.CommonItemView;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.adapter.MoveProductAdapter;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.net.bean.MoveProductBean;
import com.dlc.yunguang.net.protocol.CountingProto;
import com.dlc.yunguang.R;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 移库2
 */
public class MoveWarehouse2Activity extends BaseActivity {

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.btn_select_all) Button mBtnSelectAll;
    @BindView(R.id.item_where) CommonItemView mItemWhere;
    @BindView(R.id.recycler_view) RecyclerView mRecyclerView;
    @BindView(R.id.btn_move) Button mBtnMove;

    private MoveProductAdapter<MoveProductBean.DataBean> mAdapter;
    private String warehouseId;
    private String wareFrameId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTitleBar.leftExit(this);

        Intent intent = getIntent();
        String wareHouse = intent.getStringExtra("wareHouse");
        String wareFrame = intent.getStringExtra("wareFrame");
        warehouseId = intent.getStringExtra("warehouseId");
        wareFrameId = intent.getStringExtra("wareFrameId");

        mItemWhere.setLabel(wareHouse );

        mTitleBar.setTitle("移库");

        initData(warehouseId,wareFrameId);

        mAdapter = new MoveProductAdapter<>();

        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, BaseRecyclerAdapter.CommonHolder holder,
                int position) {
                mAdapter.selectIt(position);
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        // TODO: 2017/7/19 临时代码 ------end

    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_move_warehouse2;
    }

    @OnClick({ R.id.btn_select_all, R.id.btn_move })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_select_all:
                mAdapter.switchSelectAll();
                break;
            case R.id.btn_move:
                StringBuilder sb = new StringBuilder();
                if (mAdapter.getSelectData().size() == 0){
                    showToast("数据不能为空");
                    return;
                }
                 for(MoveProductBean.DataBean bean : mAdapter.getSelectData()){
                     sb.append(bean.getRfids()+",");
                 }
                 Intent intent = new Intent(this,MoveToActivity.class);
                intent.putExtra("rfids",sb.toString());
                intent.putExtra("warehouseId",warehouseId);
                intent.putExtra("wareFrameId",wareFrameId);
                startActivity(intent);
                break;
        }
    }

    /**
     * 获取货架数据
     */
    private void initData(String warehouseId,String wareFrameId) {
        showWaitingDialog("请等待",false);
        CountingProto.get().getWareFrameSpecMove(warehouseId,wareFrameId).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MoveProductBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogPlus.e(e.getMessage());
                        dismissWaitingDialog();
                    }

                    @Override
                    public void onNext(MoveProductBean moveProductBean) {
                        dismissWaitingDialog();
                        if(moveProductBean != null && moveProductBean.getData() != null){
                            mAdapter.setNewData(moveProductBean.getData());
                        }
                    }
                });
    }
}
